package com.augurit.gzcgwsp.ywcmn.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.printDoc.web.form.YwPrintDocForm;
import com.augurit.gzcgwsp.wfex.bi.service.IWfTemplateAttachmentRefServiceEX;
import com.augurit.gzcgwsp.wfex.bi.web.form.WfTemplateAttachmentRefFormEX;
import com.augurit.gzcgwsp.ywcmn.service.IYwDzgzListService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwDzgzListForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.common.entity.WfBusInstance;
import com.augurit.wf.common.web.WfBusClientAction;
import com.google.gson.Gson;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-dzgz-list.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/apabiPrint/list.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/dzgz/YwDzgzList/YwDzgzList-input.jsp")
})
public class YwDzgzListAction  extends WfBusClientAction<YwDzgzListForm>{
	@Autowired
	private IWfTemplateAttachmentRefServiceEX wfTemplatePrintRefServiceEX;
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	private YwDzgzListForm form;
	private Long id;
	private Long[] checkedIds;
	private Long ajid;
	private Page<YwDzgzListForm> page = new Page<YwDzgzListForm>(10);
	
	private YwPrintDocForm printDocform;	//公告板form对象
	private Long ywId;

	@Autowired
	private IYwPrintDocService ywPrintDocService;	//公告板service类
	
	@Autowired
    private IYwDzgzListService ywDzgzListService;
	
	public String getByYwid() throws Exception {
		String templateCode=getRequest().getParameter("templateCode");
		Long ywId=this.getYwId();
		List<YwPrintDocForm> list=null;
		List<YwPrintDocForm> resultList=new ArrayList<YwPrintDocForm>();
		String formatDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		if(ywId!=null){
			list=ywPrintDocService.getByYwid(ywId,templateCode);
			WfBusInstance inst=this.ywPrintDocService.getWfBusInstance(templateCode, ywId);
			String sqdw=inst.getBusMemo1();
			String acceptNo=inst.getBusMemo3();
			
			for(YwPrintDocForm form:list)
			{
				Long templateId = wfBusTemplateService.getByTemplateCode(templateCode).getId();
		    	
				WfTemplateAttachmentRefFormEX refform=wfTemplatePrintRefServiceEX.getFormByCode(form.getFileCode(),templateId);
				String fileName=refform.getFileName();
				form.setFileName(fileName);
				//String realname=fileName.substring(0,fileName.indexOf("."));
				form.setGzName(acceptNo+sqdw+fileName);
				form.setFileName(fileName);
				resultList.add(form);
			}
		}
		Gson g=new Gson();
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		System.out.println(g.toJson(resultList).toString());
		response.getWriter().print(g.toJson(resultList));	
		return null;
	}
	public String listByAjid() throws Exception{
		page=ywDzgzListService.search(page,ajid);
		List<YwDzgzListForm> list=page.getResult();
		
//		StringBuilder builder=new StringBuilder();
//		builder.append("<table align='center' border='1' align='center'><tr  align='center'><th>打印列表</th></tr>");
//		for (int i = 0; i < list.size(); i++) {
//			YwDzgzListForm f=list.get(i);
//			builder.append("<tr align='center'><td><a href='#' onclick=\"PreviewNetCEB('");
//			builder.append("http://localhost:8080/gzcgwsp");
//			builder.append(f.getFilepath());
//			builder.append("')\">");
//			builder.append(f.getFilename());
//			builder.append("</a></td></tr>");
//		}
//		builder.append("<table>");

		Gson g=new Gson();
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.getWriter().print(g.toJson(list));
		
		
		System.out.println("==listByAjid=="+g.toJson(list));
//		getRequest().setAttribute("list",list);
		return null;
	}
	public YwDzgzListForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwDzgzListForm> getPage() {
		return page;
	}


	public void setPage(Page<YwDzgzListForm> page) {
		this.page = page;
	}
	@Override
	public String wfBusSave() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String deleteMore() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	public YwDzgzListForm getForm() {
		return form;
	}
	public void setForm(YwDzgzListForm form) {
		this.form = form;
	}
	public IYwDzgzListService getYwDzgzListService() {
		return ywDzgzListService;
	}
	public void setYwDzgzListService(IYwDzgzListService ywDzgzListService) {
		this.ywDzgzListService = ywDzgzListService;
	}
	public Long getAjid() {
		return ajid;
	}
	public void setAjid(Long ajid) {
		this.ajid = ajid;
	}
	public YwPrintDocForm getPrintDocform() {
		return printDocform;
	}
	public void setPrintDocform(YwPrintDocForm printDocform) {
		this.printDocform = printDocform;
	}
	public IYwPrintDocService getYwPrintDocService() {
		return ywPrintDocService;
	}
	public void setYwPrintDocService(IYwPrintDocService ywPrintDocService) {
		this.ywPrintDocService = ywPrintDocService;
	}
	public Long getYwId() {
		return ywId;
	}
	public void setYwId(Long ywId) {
		this.ywId = ywId;
	}
	
}
